Skip to content

[Repo Assist] feat(stdlib): add collections module bindings (Counter, defaultdict, deque, OrderedDict)#272

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/bindings-collections-2026-04-22-044545f06577b2da
Draft

[Repo Assist] feat(stdlib): add collections module bindings (Counter, defaultdict, deque, OrderedDict)#272
github-actions[bot] wants to merge 1 commit intomainfrom
repo-assist/bindings-collections-2026-04-22-044545f06577b2da

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated pull request from Repo Assist, an AI assistant for this repository.

Summary

Adds F# bindings for the four most widely used classes from Python's [collections]((docs.python.org/redacted) module.

Class What it does
Counter<'T> Count hashable objects; .most_common(), .elements(), .total(), .update(), .subtract()
defaultdict<'TKey, 'TValue> Dict that calls a factory for missing keys; .contains() checks without triggering factory
deque<'T> O(1) double-ended queue with .append/.appendleft, .pop/.popleft, .rotate, bounded maxlen
OrderedDict<'TKey, 'TValue> Dict with .move_to_end() and order-sensitive .popitem()

Usage examples

open Fable.Python.Collections

// Counter
let c = Counter.ofSeq ["a"; "b"; "a"; "c"; "a"]
c.Item("a")            // 3
c.most_common(2)       // seq [("a", 3); ("b", 1)]
c.total()              // 5

// defaultdict
let d = defaultdict<string, ResizeArray<int>>(fun () -> ResizeArray())
let lst = d.Item("key")  // creates empty list automatically
lst.Add(42)

// deque
let q = deque.ofSeq [1; 2; 3]
q.appendleft(0)        // deque [0; 1; 2; 3]
q.pop()                // 3
let bounded = deque<int>.withMaxlen(3)

// OrderedDict
let od = OrderedDict<string, int>()
od.set("a", 1); od.set("b", 2)
od.move_to_end("a")    // keys: ["b"; "a"]

Files changed

  • src/stdlib/Collections.fs — new bindings file
  • test/TestCollections.fs — 35 tests covering all four types
  • src/Fable.Python.fsproj — include new source file
  • test/Fable.Python.Test.fsproj — include new test file

Design notes

  • Uses [<Import("ClassName", "collections")>] pattern, consistent with Queue, datetime, etc.
  • Static factory methods (Counter.ofSeq, deque.ofSeq, deque.withMaxlen) handle constructor variants rather than adding extra primary constructor overloads, which follows the pattern established in Datetime.fs.
  • defaultdict.get and OrderedDict.get do not invoke the factory — behaviour matches Python exactly.
  • No existing signatures changed; this is a purely additive PR.
  • Note: CHANGELOG.md is intentionally not updated per repository policy.

CI will validate the build and Python tests.

Note

🔒 Integrity filter blocked 10 items

The following items were blocked because they don't meet the GitHub integrity level.

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

Generated by Repo Assist · ● 2.3M ·

…deque, OrderedDict)

Adds F# bindings for the most commonly used classes in Python's collections
module:

- Counter<'T>: count hashable objects; supports most_common, elements,
  total, update, subtract; static Counter.ofSeq factory
- defaultdict<'TKey, 'TValue>: dict with callable factory for missing keys;
  includes contains helper to check key presence without invoking factory
- deque<'T>: O(1) double-ended queue with append/appendleft, pop/popleft,
  rotate, and optional bounded maxlen; static ofSeq and withMaxlen factories
- OrderedDict<'TKey, 'TValue>: dict subclass with move_to_end and
  order-sensitive popitem

Also adds 35 tests covering construction, mutation, and edge cases for all
four types.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants